import 'dart:convert';

import 'package:number_spark_flutter/model/ns_user_info_model.dart';
import 'package:number_spark_flutter/network/ns_network_utils.dart';
import 'package:number_spark_flutter/utils/ns_md5_utils.dart';
import 'package:sp_util/sp_util.dart';

import 'ns_record_voice_utils.dart';

class NSUserUtils {
  ///是否登录
  static bool isLogin() {
    return NSNetWorkUtils.getAccessToken().isNotEmpty;
  }

  static Future<bool> saveUserInfo(NSUserInfoModel userInfo) async {
    return await SpUtil.putObject('userInfo', userInfo) ?? false;
  }

  static NSUserInfoModel getUserInfo() {
    return SpUtil.getObj('userInfo', (v) => NSUserInfoModel.fromJson(v)) ?? NSUserInfoModel();
  }

  static String getId() {
    return getUserInfo().userId ?? '';
  }

  static String getNickName() {
    return getUserInfo().nickname ?? '';
  }

  static String getAvatar() {
    return getUserInfo().avatarUrl ?? '';
  }

  static Future<bool> setAppConfig(Map appConfig) async {
    return await SpUtil.putString('appConfig', jsonEncode(appConfig)) ?? false;
  }

  static Map getAppConfig() {
    Map map = jsonDecode(SpUtil.getString('appConfig', defValue: '{}') ?? '{}');
    return map;
  }

  static getUserDoubleGameFreeCount() {
    return SpUtil.getInt('doubleGameFreeCount_${NSUserUtils.getId()}', defValue: 10);
  }

  static setUserDoubleGameFreeCount(int count) {
    SpUtil.putInt('doubleGameFreeCount_${NSUserUtils.getId()}', count);
  }

  static getIsChangedAvatar() {
    return SpUtil.getBool('isChangedAvatar_${getId()}', defValue: false);
  }

  static setIsChangedAvatar(bool isChanged) {
    SpUtil.putBool('isChangedAvatar_${getId()}', isChanged);
  }

  static NSRecordVoiceModel? getUserRecordModel() {
    return SpUtil.getObj('recordModel_${getId()}', (v) => NSRecordVoiceModel.fromJson(v));
  }

  static Future setUserRecordModel(NSRecordVoiceModel model) async {
    return await SpUtil.putObject('recordModel_${getId()}', model);
  }

  static clearUserRecordModel() {
    SpUtil.remove('recordModel_${getId()}');
  }
}
